<?php

/**
 * Description of DesignationsController
 *
 * @author Poh Nean
 */
class HierarchyController extends PsController
{

	public $layout = 'manage';

	public function moduleAccessRules()
	{
		return array(
			array('Propsage.Accounting.Commission')
		);
	}

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'actions' => array('index'),
						'roles' => array('ManageAgency.Designation.View'),
					),
						), parent::accessRules());
	}

	public function actionIndex()
	{
		
		$criteria = new CDbCriteria();
		$criteria->order = 't.from_date DESC';
		$criteria->addInCondition('t.agency_id', Yii::app()->user->getParentAgencyIds());
		$hierarchies = Hierarchy::model()->findAll($criteria);

		$data = array();
		$data['hierarchies'] = $hierarchies;
		$this->render('index', $data);
	}

	public function loadHierarchy($id)
	{
		$hierarchy = Hierarchy::model()->ofCurrentAgency()->findByPk($id);

		if ($hierarchy === null) {
			throw new CHttpException(404, "Oops! The page you requested is not found.");
		}

		return $hierarchy;
	}

	public function getDesignationDataProvider($id)
	{
		$criteria = new CDbCriteria;
		$criteria->join = "JOIN `hierarchy` h ON t.hierarchy_id = h.id";
		$criteria->compare('t.hierarchy_id', $id);
		$criteria->compare('t.deleted', 0);
		$criteria->order = 't.pos DESC';

		$page = new CPagination;
		$page->pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 20;

		$dataProvider = new CActiveDataProvider('Designation', array('pagination' => $page));
		$dataProvider->setCriteria($criteria);

		return $dataProvider;
	}

}

?>
